package com.xy.bizportdemo.util;

import android.text.TextUtils;
import android.util.LruCache;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间格式转换工具类
 * Created by XIAOYUAN on 2017/3/2.
 */

public class TimeCacheUtil {
    private static TimeCacheUtil instance;
    private static LruCache<Long, String> timeConvertMap = new LruCache<Long, String>(100);

    private TimeCacheUtil() {

    }

    public static synchronized TimeCacheUtil getInstance() {
        if (instance == null) {
            instance = new TimeCacheUtil();
        }
        return instance;
    }

    public String formateTime(long timeStamp){
        String strTimeVal = timeConvertMap.get(timeStamp);
        if (TextUtils.isEmpty(strTimeVal)){
            strTimeVal = getFormatDate(timeStamp);
            timeConvertMap.put(timeStamp, strTimeVal);
        }
        return strTimeVal;
    }

    private String getFormatDate(long time) {
        Date date = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("MM-dd HH:mm");
        return sf.format(date);
    }
}
